//
//  DetailNoticeViewController.m
//  EMMApp
//
//  Created by Mac on 16/7/20.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "DetailNoticeViewController.h"
#import "GetNoticeDetailApi.h"
#import "DetailNoticeCell.h"

@interface DetailNoticeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

//公告model
@property (nonatomic, strong) GetNoticeDetail *getNoticeDetail;

@end

@implementation DetailNoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self postGetNoticeDetailApi];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatNavigationController];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 导航
- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = _titleString;
    [self configLeftBtn];
}

- (void)postGetNoticeDetailApi {
    [self showHudInWindow:@"正在加载请稍等..."];
    GetNoticeDetailApi *api = [[GetNoticeDetailApi alloc] initWithNoticeId:self.noticeId];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetNoticeDetailApi *result = (GetNoticeDetailApi *)request;
        if (result.isSuccess) {
            GetNoticeDetail *get = result.resultData;
            _getNoticeDetail = get;
            [_tableView reloadData];
        } else {
            [self showToast:result.resultData.errorMsg];
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}
#pragma mark - UITableView 代理

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return EMM_HEIGHT-SafeAreaTopHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellId = @"DetailNoticeCellId";
    DetailNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DetailNoticeCell" owner:self options:nil] lastObject];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleNaem.text = _getNoticeDetail.noticeTitle;
    cell.timerLabel.text = _getNoticeDetail.noticeDate;
    cell.subTItleName.text = _getNoticeDetail.noticeContent;
    return cell;
}

@end
